package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"user-center/src/common"
	"user-center/src/model"
	"user-center/src/model/request"
	"user-center/src/model/response"
	"user-center/src/service"
)

// TeamAdd
// @Tags        team
// @Summary     创建队伍
// @Description   用户只能创建5个队伍
// @Param TeamCreate body request.TeamAddRequest true "TeamCreate"
// @Router /team/add [post]
func TeamAdd(ctx *gin.Context) {
	req := &request.TeamAddRequest{}
	err := parsePostJsonRequest(ctx, req)
	if err != nil {
		return
	}
	//处理请求
	currentUser := service.GetUserCurrentInfo(ctx)
	team := &model.Team{
		Password:    req.Password,
		UserId:      req.UserId,
		Name:        req.Name,
		Description: req.Description,
		ExpireTime:  req.ExpireTime,
		Status:      req.Status,
		MaxNum:      req.MaxNum,
	}
	resTeam := service.TeamAdd(ctx, team, currentUser)
	if resTeam.Code != 0 {
		ctx.JSON(http.StatusOK, resTeam)
		return
	}
	resGroup := service.GroupCreate(ctx, team.Id, team.Name)
	if resGroup.Code != 0{
		ctx.JSON(http.StatusOK, resGroup)
		return 
	}
	res := &response.BaseResponse{}
	ctx.JSON(http.StatusOK , res)
}

// TeamUpdate
// @Tags        team
// @Summary     更新队伍
// @Description   只有队长可以更新
// @Param TeamUpdate body request.TeamUpdateRequest true "TeamUpdate"
// @Router /team/update [post]
func TeamUpdate(ctx *gin.Context) {
	reqTeamUpdate := &request.TeamUpdateRequest{}
	res := &response.BaseResponse{}
	err := parsePostJsonRequest(ctx, reqTeamUpdate)
	if err != nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		ctx.JSON(http.StatusOK, res)
		return
	}
	res = service.UpdateTeam(ctx, reqTeamUpdate, service.GetUserCurrentInfo(ctx))
	ctx.JSON(http.StatusOK, res)
}

// TeamGetById
// @Tags        team
// @Summary     通过队伍Id获取队伍详细信息
// @Description   通过队伍Id获取队伍详细信息
// @Param id query int true "id"
// @Router /team/get [get]
func TeamGetById(ctx *gin.Context) {
	teamId, _ := strconv.ParseInt(ctx.Query("id"), 10, 64)
	res := &response.BaseResponse{}
	if teamId <= 0 {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		ctx.JSON(http.StatusOK, res)
		return
	}
	ctx.JSON(http.StatusOK, service.GetTeamById(ctx, teamId))

}

// TeamLists GET
// @Tags        team
// @Summary     获取队伍列表
// @Description   可以增加查询条件
// @Param teamQuery query request.TeamQuery false "teamQuery"
// @Router /team/list [get]
func TeamLists(ctx *gin.Context) {
	res := &response.BaseResponse{}
	teamQuery := &request.TeamQuery{}
	err := parseGetRequest(ctx, teamQuery)
	if err != nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		ctx.JSON(http.StatusOK, res)
		return
	}
	isAdmin := service.IsAdmin(ctx)
	ctx.JSON(http.StatusOK, service.ListTeams(ctx, teamQuery, isAdmin))
}

// TeamListsByPage GET
// @Tags        team
// @Summary     获取队伍列表分页[todo]
// @Description   可以增加查询条件
// @Router /team/list/page [get]
func TeamListsByPage(ctx *gin.Context) {}

// TeamJoin
// @Tags        team
// @Summary     加入队伍
// @Description   用户加入队伍
// @Param teamJoin body request.TeamJoinRequest true "teamJoin"
// @Router /team/join [post]
func TeamJoin(ctx *gin.Context) {
	res := &response.BaseResponse{}
	req := &request.TeamJoinRequest{}
	err := parsePostJsonRequest(ctx, req)
	if err != nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		ctx.JSON(http.StatusOK, res)
		return
	}
	ctx.JSON(http.StatusOK, service.JoinTeam(ctx, req, service.GetUserCurrentInfo(ctx)))
}

// TeamQuit
// @Tags        team
// @Summary     退出队伍[todo]
// @Description   用户退出队伍
// @Router /team/quit [post]
func TeamQuit(ctx *gin.Context) {
	req := &request.TeamQuitRequest{}
	res := &response.BaseResponse{}
	err := parsePostJsonRequest(ctx, req)
	if err != nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		ctx.JSON(http.StatusOK, res)
		return
	}
	ctx.JSON(http.StatusOK, service.QuitTeam(ctx, req, service.GetUserCurrentInfo(ctx)))
}

// TeamDelete
// @Tags        team
// @Summary     解散队伍[todo]
// @Description   解散队伍
// @Router /team/delete [post]
func TeamDelete(ctx *gin.Context) {
	res := &response.BaseResponse{}
	req := &request.TeamDeleteRequest{}
	err := parsePostJsonRequest(ctx, req)
	if err != nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		ctx.JSON(http.StatusOK, res)
		return
	}
	ctx.JSON(http.StatusOK, service.DeleteTeam(ctx, req, service.GetUserCurrentInfo(ctx)))
}

// TeamListsByMyCreated
// @Tags        team
// @Summary     查看用户创建的队伍
// @Description   可以根据条件查询
// @Param myCreate query request.TeamQuery false "myCreate"
// @Router /team/list/my/create [get]
func TeamListsByMyCreated(ctx *gin.Context) {
	res := &response.BaseResponse{}
	teamQuery := &request.TeamQuery{}
	err := parseGetRequest(ctx, teamQuery)
	if err != nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		ctx.JSON(http.StatusOK, res)
		return
	}
	currentUser := service.GetUserCurrentInfo(ctx)
	if currentUser == nil {
		res.ResponseWithError(common.NOT_LOGIN, "未登录")
		ctx.JSON(http.StatusOK, res)
		return
	}
	teamQuery.UserId = currentUser.Id
	ctx.JSON(http.StatusOK, service.ListTeams(ctx, teamQuery, true))
}

// TeamListJoinInMy
// @Tags        team
// @Summary     查看用户加入的队伍
// @Description   可以根据条件查询
// @Param myJoin query request.TeamQuery false "myJoin"
// @Router /team/list/my/join [get]
func TeamListJoinInMy(ctx *gin.Context) {
	res := &response.BaseResponse{}
	teamQuery := &request.TeamQuery{}
	err := parseGetRequest(ctx, teamQuery)
	if err != nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		ctx.JSON(http.StatusOK, res)
		return
	}
	ctx.JSON(http.StatusOK, service.ListTeamsJoinInMy(ctx, teamQuery, service.GetUserCurrentInfo(ctx)))
}
